﻿using BLL;
//using BLL.Bar;
using Entity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Reflection.Emit;
using System.Security.Cryptography;
using YunProject.Controllers.Base;
using static Azure.Core.HttpHeader;

namespace YunProject.Controllers
{
    public class BasicController : BaseController
    {
        InventoryBLL venBLL;
        BasicBLL basicBLL;

        public BasicController()
        {
            venBLL = new InventoryBLL();
            basicBLL = new BasicBLL();
        }

        #region
        [Authorize]
        public ActionResult InventoryInfo()
        {
            return View();
        }
        public ActionResult SelectInventory()
        {
            return View();
        }
        public ActionResult SelectCustomer()
        {
            return View();
        }
        public ActionResult SelectGY()
        {
            return View();
        }
        [Authorize]
        public ActionResult CommonDataInfo()
        {
            return View();
        }
        public ActionResult CommonDataUpdate()
        {
            return View();
        }
        [Authorize]
        public ActionResult WareHouseInfo()
        {
            return View();
        }
        [Authorize]
        public ActionResult CustomerInfo()
        {
            return View();
        }
        [Authorize]
        public ActionResult BomProduction()
        {
            return View();
        }
        public ActionResult BomUpdate()
        {
            return View();
        }
        public ActionResult SelectProType() 
        {
            return View();
        }
    
        public ActionResult BomBanGunAdd()
        {
            return View();
        }
        [Authorize]
        public ActionResult BanGunInfo()
        {
            return View();
        }
        [Authorize]
        public ActionResult SetBanGunPro()
        {
            return View();
        }
        [Authorize]
        public ActionResult BanGunDetails()
        {
            return View();
        }
        [Authorize]
        public ActionResult SelectBanGun()
        {
            return View();
        }
        [Authorize]
        public ActionResult SelectRdrecord11()
        {
            return View();
        }
        [Authorize]
        public ActionResult SelectInvStock()
        {
            return View();
        }
        #endregion



        [HttpPost]
        public JsonResult CuClassNavigation(string dataType)
        {
            var rsl = new ReturnResult();
            CustomBLL cuBLL = new CustomBLL();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = cuBLL.TreeClassNavigation(userObj["baseCode"].ToString(), dataType);
            }
            return Json(rsl);
        }

        [HttpPost]
        public JsonResult GetCommonData(string type)
        {
            return Json(basicBLL.GetCommonData(type));
        }

        [HttpPost]
        public JsonResult getCommonDataTop()//通用数据数据大类
        {
            return Json(basicBLL.getCommonDataTop());
        }

        [HttpPost]
        public JsonResult getCommonDataType(string type)//通用数据类型
        {
            return Json(basicBLL.getCommonDataType(type));
        }
        [HttpPost]
        public JsonResult UpdateCommonData(string datas)
        {
            var rsl = new ReturnResult();
            CustomBLL cuBLL = new CustomBLL();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = basicBLL.UpdateCommonData(userObj["userCode"].ToString(), datas);
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult SaveBanGunPro(string datas)
        {
            var rsl = new ReturnResult();
            CustomBLL cuBLL = new CustomBLL();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = basicBLL.SaveBanGunPro(datas,userObj["userCode"].ToString());
            }
            return Json(rsl);
        }
        
        [HttpPost]
        public JsonResult DelCommonData(string dataId)
        {
            var rsl = new ReturnResult();
            CustomBLL cuBLL = new CustomBLL();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = basicBLL.DelCommonData(userObj["userCode"].ToString(), dataId);
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult getCommonDataById(string dataId)//通过Id获得通用数据信息
        {
            return Json(basicBLL.getCommonDataById(dataId));
        }
        [HttpPost]
        public JsonResult EditCommonNames(string dataId, string names)
        {
            return Json(basicBLL.EditCommonNames(dataId, names));
        }

   
        [HttpPost]
        public JsonResult UpdateBomData(string datas)
        {
            var rsl = new ReturnResult();
            CustomBLL cuBLL = new CustomBLL();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = basicBLL.UpdateBomData(datas,userObj["userCode"].ToString());
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult DelBanYangPro(string dataId,string byCode)
        {
            var rsl = new ReturnResult();
            CustomBLL cuBLL = new CustomBLL();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = basicBLL.DelBanYangPro(dataId,byCode,userObj["userCode"].ToString());
            }
            return Json(rsl);
        }
        
    

        [HttpPost]
        public JsonResult GetByBarCode(string type)
        {
            return Json(basicBLL.GetByBarCode(type));
        }

        [HttpPost]
        public JsonResult GetBomMainByCode(string bomCode)
        {
            return Json(basicBLL.GetBomMainByCode(bomCode));
        }
        

        //[HttpPost]
        //public JsonResult AddBomTemp(string proCodes, string proNames)//bom临时表数据添加
        //{
        //    return Json(basicBLL.AddBomTemp(proCodes, proNames));
        //}
        //[HttpPost]
        //public JsonResult venCustomClass(string dataType)
        //{
        //    var rsl = new ReturnResult();
        //    var userObj = LoginInfo();
        //    if (userObj != null)
        //    {
        //        rsl = basicBLL.venCustomClass(userObj["baseCode"].ToString(),dataType);
        //    }
        //    return Json(rsl);

        //    //return Json(basicBLL.EditCommonNames(dataId, names));
        //}

        public JsonResult getDataList(string page, string limit, string condition, string dataType)
        {
            var rsl = new ReturnResult();
            string pageCount = "1";
            string msg = "";
            var datas = new object();
            var userObj = LoginInfo();
            if (dataType == "ven")
            {
                rsl = venBLL.InventoryList(page, limit, condition, userObj["baseCode"].ToString(), ref pageCount);
            }
            else if (dataType == "wareHouse")
            {
                WareHouseBLL wareBLL = new WareHouseBLL();
                rsl = wareBLL.getWareHouseList(page, limit, ref pageCount, condition, userObj["baseCode"].ToString());
            }
            else if (dataType == "wp")
            {
                //货位列表
                WareHouseBLL wareBLL = new WareHouseBLL();
                rsl = wareBLL.WarePositionList(userObj["baseCode"].ToString(), condition);
            }
            else if (dataType == "Cu")
            {
                CustomBLL cuBLL = new CustomBLL();
                rsl = cuBLL.getCustomDataList(userObj["baseCode"].ToString(), page, limit, ref pageCount, condition);
            }
            else if (dataType == "cmd")
            {
                rsl = basicBLL.getCommonDataList(userObj["baseCode"].ToString(), page, limit, ref pageCount, condition);
            }
            else if (dataType == "rd11")
            {
                rsl = basicBLL.Rdrecords11List(page, limit, condition, userObj["baseCode"].ToString(), ref pageCount);    //材料出库单
            }
            else if (dataType == "bgl")
            {
          
             rsl = basicBLL.GetBanGunList(page, limit, condition, userObj["isAdmin"].ToString(), userObj["baseCode"].ToString(), ref pageCount);
            
            }
            else if (dataType == "bypd")
            {
                rsl = basicBLL.GetBanYangDetails(condition);
            }
            else if (dataType == "bom")
            {
                rsl = basicBLL.GetBomMainList(page, limit, condition, userObj["baseCode"].ToString(), ref pageCount);
            }
            else if (dataType == "bmm")
            {
                rsl = basicBLL.GetBomMaterialList(page, limit, ref pageCount, condition, userObj["baseCode"].ToString());
            }
            else if (dataType == "byri")
            {
                rsl = basicBLL.BanGunInOutRecords(page, limit,  condition, userObj["baseCode"].ToString(), ref pageCount);
            }
            
            else if (dataType == "bmb")
            {
                rsl = basicBLL.GetBomBanGun(page, limit, condition, ref pageCount);
            }
            else if (dataType == "sbgl")
            {
                rsl = basicBLL.GetBabGunByCode(page, limit, condition,ref pageCount);
            }
            else if (dataType == "venStock")
            {
                rsl = venBLL.getStockList( userObj["baseCode"].ToString(), condition);    //现存量
            }

            if (rsl.Result)
            {
                datas = rsl.Data;
            }
            else
            {
                msg = rsl.Message;
            }
            var dic = new Dictionary<string, object>();
            dic["count"] = pageCount;
            dic["msg"] = msg;
            dic["code"] = "0";
            dic["data"] = datas;
            return new JsonResult(dic);

        }

        [HttpPost]
        public JsonResult CommonDataTree(string dataType)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = basicBLL.CommonDataTree(dataType, userObj["baseCode"].ToString());
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult GetProductByCode(string proCode)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl =venBLL.GetProductByCode(proCode);
            }
            return Json(rsl);
        }

        
        [HttpPost]
        public JsonResult EditBomSunMaterial(string dataId, string fieldName, string fieldValue)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = basicBLL.EditBomSunMaterial(dataId, fieldName,fieldValue);
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult DelBomSubDataById(string dataId)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = basicBLL.DelBomSubDataById(dataId, userObj["baseCode"].ToString());
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult DelBomInfoByCode(string bomCode)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = basicBLL.DelBomInfoByCode(bomCode, userObj["baseCode"].ToString());
            }
            return Json(rsl);
        }
        
        [HttpPost]
        public JsonResult AddBomSubDatas(string proCodes,string bomCode,string dType)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = basicBLL.AddBomSubDatas(proCodes,bomCode,dType ,userObj["baseCode"].ToString());
            }
            return Json(rsl);
        }
        
        /// <summary>
        /// 出入库类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult getDictionaryInfo(string type)
        {
            //BasicBLL BasBLL = new BasicBLL();
            return Json(basicBLL.getDictionaryInfo(type, ""));
        }

        /// <summary>
        /// 生产工序
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult getCommonDataInfo(string type)
        {
            return Json(basicBLL.getCommonDataInfo(type, ""));
        }

        /// <summary>
        /// 删除板棍信息
        /// </summary>
        /// <param name="byCode"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult DelBanGun(string byCode)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = basicBLL.DelBanGun(byCode, userObj["baseCode"].ToString());
            }
            return Json(rsl);
        }

        /// <summary>
        /// 获得材料车间仓库
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetWareData(string type)
        {
            return Json(basicBLL.GetWareData(type));
        }

    }
}
